<?php
namespace app\common\loglic;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class Alisms
{
    private $error = '';

    //获取错误信息
    public function getError()
    {
        return $this->error;
    }
    
    //发送短信验证码接口
    public function captcha($phoneNumbers='', $smsContent='')
    {
        AlibabaCloud::accessKeyClient(config('apis.aliyun_key'), config('apis.aliyun_secret'))->regionId(config('apis.aliyun_region'))->asGlobalClient();

        //短信模板变量对应的实际值
        $templateParam = json_encode([
            config('apis.aliyun_template_param') => $smsContent
        ],JSON_UNESCAPED_UNICODE);

        //发送接口
        try{
            $result = AlibabaCloud::dysmsapi()
                ->v20170525()
                ->sendSms()
                ->withPhoneNumbers($phoneNumbers)
                ->withSignName(config('apis.aliyun_sign_name'))
                ->withTemplateCode(config('apis.aliyun_template_code'))
                ->withTemplateParam($templateParam)
                ->request()
                ->toArray();
            //请求状态码
            if($result['Code'] == 'OK'){
                return true;
            }else{
                $this->error = $result['Message'];
            }
        }catch(ClientException $exception){
            $this->error = $exception->getErrorMessage();
        }catch(ServerException $exception){
            $this->error = $exception->getErrorMessage();
        }
        return false;
    }
}